
import Toast from '@vant/weapp/toast/toast';
import { clearUserLocalStorage } from "../../../../utils/util";
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        view_type: 0,
        view_text: '[全天]',
        name: '',
        summary_list: [],

        current_delivery_man_id: 0,
        current_delivery_district_id: 0,
        current_delivery_man_name: '',

        is_print: false
    },

    formatQuantity(quantity) {
        if (Number.isInteger(quantity)) {
            return quantity.toString();
        } else {
            return quantity.toFixed(2);
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */

    async onLoad(options) {
        let now = (new Date().getTime()) / 1000;
        let expiration_time = app.session.expiration_time ?? 0
        if (now > expiration_time) {
            console.log('已过登录有效期!')
            app.session = {}
            clearUserLocalStorage()
            wx.reLaunch({
                url: '/pages/selectID/selectID',
            })
        }

        console.log('session:', app.session)
				// 从 app 会话中获取配送员级别
				let level = wx.getStorageSync('level')
        let access_token = app.session.access_token
        let delivery_man = app.session.delivery_man
        let current_delivery_man_id = app.session.current_delivery_man_id
        let delivery_man_list = app.session.delivery_man_list
        let delivery_district_list = app.session.delivery_district_list
				let delivery_list = wx.getStorageSync('delivery_list')
				console.log(delivery_man)
        this.setData({
            access_token,
            delivery_man,
            current_delivery_man_id,
            delivery_man_list: delivery_man_list,
            delivery_district_list: delivery_district_list,
            current_delivery_district_id: 0,
            level,
        })

        this.getGoodsSummary(0, '[全部]', 0)
    },

    async viewAll() {
        this.getGoodsSummary(0, '[全部]', 0)
    },

    async viewInDelivery() {
        this.getGoodsSummary(1, '[未配送]', 0)
    },
    async viewDelivered() {
        this.getGoodsSummary(2, '[已配送]', 0)
    },

    async printSummary() {
        let view_type = this.data.view_type
        let view_text = this.data.view_text
        this.getGoodsSummary(view_type, view_text, 1)
    },

    //根据订单id查询对应的订单信息
    async getGoodsSummary(view_type = 0, view_text = '[全部]', print_type = -1) {
        that = this
        let delivery_man = wx.getStorageSync('delivery_man');
        let delivery_man_id = delivery_man.delivery_man_id

        let current_delivery_man_id = that.data.current_delivery_man_id
        let current_delivery_man_name = that.data.current_delivery_man_name
        let current_delivery_district_id = that.data.current_delivery_district_id
        let that = this;
        wx.p.request({
            url: app.globalData.API + '/delivery/index/get-goods-summary',
            header: {
                'Content-Type': 'application/json',
                "x-api-key": this.data.access_token,
            },
            method: "POST",
            data: {
                current_delivery_man_id,
                view_type: view_type,
                print_type: print_type,
            },

            success(res) {
                let summary_list = res.data.data.summary_list

                summary_list.forEach(item => {
                    // 判断goods_no属性的前缀
                    if (item.attr_no.startsWith('GH')) {
                        // 如果以GH开头，则添加干货标签，标签颜色为黄色
                        item.tag = '干货';
                        item.tagColor = 'warning';
                    } else if (item.attr_no.startsWith('LC')) {
                        // 如果以LC开头，则添加冷藏标签，标签颜色为蓝色
                        item.tag = '冷藏';
                        item.tagColor = 'primary';
                    }
                });

                let print_data = res.data.data.print_data
                let is_print = false
                if (print_data && print_data.msg === 'ok') {
                    is_print = true
                    Toast('打印成功!');
                }
                that.setData({
                    summary_list: summary_list,
                    view_type: view_type,
                    view_text: view_text,
                    is_print: is_print
                })

            }
        })
    },

    on_dropdown_change(e) {
        const current_delivery_man_id = e.detail;
        let current_delivery_name = '未知'
        let current_delivery_district_id = 0

        const delivery_man = this.data.delivery_man_list.find(d => d.value === current_delivery_man_id);
        if (delivery_man) {

            current_delivery_name = delivery_man.text;
            current_delivery_district_id = delivery_man.delivery_district_id

            console.log('----', current_delivery_district_id)
        }

        this.setData({
            current_delivery_man_id: current_delivery_man_id,
            current_delivery_name: current_delivery_name,
            current_delivery_district_id: current_delivery_district_id
        });
        wx.setStorageSync('current_delivery_man_id', current_delivery_man_id);
        wx.setStorageSync('current_delivery_name', current_delivery_name);
        wx.setStorageSync('current_delivery_district_id', current_delivery_district_id);
        let view_type = this.data.view_type
        let view_text = this.data.view_text
        this.getGoodsSummary(view_type, view_text, 0)

        //this.reLoad()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})